﻿using MvcBJDCWebApp.Models;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web.Mvc;
using CQ.Framework.APIService;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
using MvcBJDCWebApp.Models.API;
using Newtonsoft.Json;

namespace MvcBJDCWebApp.Controllers
{
    public class APIRacesController : Controller
    {
        private APIRaceDBContext db = new APIRaceDBContext();
        private IAPIHelper ApiHelper;

        public APIRacesController(IAPIHelper apiHelper)
        {
            ApiHelper = apiHelper;
        }

        // GET: APIRaces
        public ActionResult Index()
        {
            GetAPIData(ApiHelper);

            return View(db.APIRaces.ToList());
        }

        // GET: APIRaces/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            APIRace aPIRace = db.APIRaces.Find(id);
            if (aPIRace == null)
            {
                return HttpNotFound();
            }
            return View(aPIRace);
        }

        // GET: APIRaces/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: APIRaces/Create
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性，有关 
        // 详细信息，请参阅 https://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "APIRaceID,APIKey,saicheng,c1,c2,c3,c4T1,c4T1URL,c4R,c4T2,c4T2URL,c51,c51Link,c52,c52Link,liveid")] APIRace aPIRace)
        {
            if (ModelState.IsValid)
            {
                db.APIRaces.Add(aPIRace);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(aPIRace);
        }

        // GET: APIRaces/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            APIRace aPIRace = db.APIRaces.Find(id);
            if (aPIRace == null)
            {
                return HttpNotFound();
            }
            return View(aPIRace);
        }

        // POST: APIRaces/Edit/5
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性，有关 
        // 详细信息，请参阅 https://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "APIRaceID,APIKey,saicheng,c1,c2,c3,c4T1,c4T1URL,c4R,c4T2,c4T2URL,c51,c51Link,c52,c52Link,liveid")] APIRace aPIRace)
        {
            if (ModelState.IsValid)
            {
                db.Entry(aPIRace).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(aPIRace);
        }

        // GET: APIRaces/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            APIRace aPIRace = db.APIRaces.Find(id);
            if (aPIRace == null)
            {
                return HttpNotFound();
            }
            return View(aPIRace);
        }

        // POST: APIRaces/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            APIRace aPIRace = db.APIRaces.Find(id);
            db.APIRaces.Remove(aPIRace);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }

        private void GetAPIData(IAPIHelper aPIHelper)
        {
            string appkey = System.Configuration.ConfigurationManager.AppSettings["apiKey"].ToString();   

            string url = "http://op.juhe.cn/onebox/football/league";

            var para = new Dictionary<string, string>();

            para.Add("key", appkey);//你申请的key
            para.Add("dtype", ""); //返回数据的格式,xml或json，默认json
            para.Add("league", "中超"); //联赛名称

            JObject json = aPIHelper.GetAPIData(appkey, url, para);

            SerJson(json);

            //db.APIRaces.Add(aPIRace);
            //db.SaveChanges();
            //return RedirectToAction("Index");
        }

        private void SerJson(JObject jsonApi)
        {
            //APIQuery Deserialize JSON Object

            //Student stu2 = JsonConvert.DeserializeObject<Student>(json1);

            // initial view object
            var views = jsonApi["result"]["views"].ToString();

            APIView apiView= JsonConvert.DeserializeObject<APIView>(views);

            //var time = jsonApi["reason"].ToString();
        }
    }
}
